package com.android.mdw.demo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements SurfaceHolder.Callback{
	
	private MediaRecorder mediaRecorder = null;
	private MediaPlayer mediaPlayer = null;
	
	private String fileName = null;	
	private boolean recording = false;
	
	    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        fileName = Environment.getExternalStorageDirectory() + "/test.mp4";
        
        SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
        SurfaceHolder holder = surface.getHolder();
        holder.addCallback(this);        
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        final Button btnRec = (Button) findViewById(R.id.btnREC);
        final Button btnStop = (Button) findViewById(R.id.btnSTOP);
        final Button btnPlay = (Button) findViewById(R.id.btnPLAY);
        
        btnRec.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				btnRec.setEnabled(false);
				btnStop.setEnabled(true);
				btnPlay.setEnabled(false);
				prepareRecorder();
				mediaRecorder.setOutputFile(fileName);
				
				try {
					mediaRecorder.prepare();
				} catch (IllegalStateException e) {		
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mediaRecorder.start();
				recording = true;
			}
			
		});
        
        btnPlay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				btnRec.setEnabled(false);
				btnPlay.setEnabled(false);
				btnStop.setEnabled(true);
				
				mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						btnRec.setEnabled(true);
						btnStop.setEnabled(false);
						btnPlay.setEnabled(true);
												
					}
				});
				
				try {
					mediaPlayer.setDataSource(fileName);
					mediaPlayer.prepare();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mediaPlayer.start();
				
			}
		});       
        
        btnStop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				btnRec.setEnabled(true);
				btnPlay.setEnabled(true);
				btnStop.setEnabled(false);
				prepareRecorder();
				
				if(recording){
					recording = false;
					mediaRecorder.stop();
					mediaRecorder.reset();					
				} else if(mediaPlayer.isPlaying()){
					mediaPlayer.stop();
					mediaPlayer.reset();
				}
				
			}
		});
        
    }

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(mediaRecorder == null){
			mediaRecorder = new MediaRecorder();
			mediaRecorder.setPreviewDisplay(holder.getSurface());
		}
		
		if(mediaPlayer == null){
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setDisplay(holder);
			
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		mediaRecorder.release();
		mediaPlayer.release();		
	}
	
	public void prepareRecorder(){
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);		
	}
}